package com.by.basicgateway.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ByteArrayResource;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.by.toolkit.utils.ExceptionUtil.getStackTrace;

/**
 * @author Tian Wei
 * @version 1.0
 * @date 2023/12/20 7:42 PM
 */

@Slf4j
public class ConfigFileConvertUtil {

    public static <T> T converterObjByYaml(String prefix, String yamlContent, Class<T> convertClazz) {

        if (StringUtils.isEmpty(yamlContent)) {
            return null;
        }

        List<PropertySource<?>> propertySources = null;
        try {
            YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
            propertySources = yamlPropertySourceLoader.load(prefix, new ByteArrayResource(yamlContent.getBytes(StandardCharsets.UTF_8)));
            StandardEnvironment environment = new StandardEnvironment();
            for (PropertySource<?> propertySource : propertySources) {
                environment.getPropertySources().addLast(propertySource);
            }

            BindResult<T> listBindResult = Binder.get(environment).bind(prefix, Bindable.of(convertClazz));
            if (Objects.isNull(listBindResult)) {
                log.info("[ConfigFileConvertUtil][converterObjectByYaml] 未匹配到相关属性,请检测 prefix:{} convertClazz:{} yamlContent:{}", prefix, convertClazz.getName(), yamlContent);
                return null;
            }

            return listBindResult.get();
        } catch (IOException e) {
            log.error("[ConfigFileConvertUtil][converterObjectByYaml] yaml配置信息加载失败:{}", getStackTrace(e));
            return null;
        }
    }

    public static <T> List<T> converterListByYaml(String prefix, String yamlContent, Class<T> convertClazz) {
        if (StringUtils.isEmpty(yamlContent)) {
            return Collections.emptyList();
        }

        List<PropertySource<?>> propertySources = null;
        try {
            YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
            propertySources = yamlPropertySourceLoader.load(prefix, new ByteArrayResource(yamlContent.getBytes(StandardCharsets.UTF_8)));
            StandardEnvironment environment = new StandardEnvironment();
            for (PropertySource<?> propertySource : propertySources) {
                environment.getPropertySources().addLast(propertySource);
            }

            BindResult<List<T>> listBindResult = Binder.get(environment).bind(prefix, Bindable.listOf(convertClazz));
            if (Objects.isNull(listBindResult)) {
                log.info("[ConfigFileConvertUtil][converterObjectByYaml] 未匹配到相关属性,请检测 prefix:{} convertClazz:{} yamlContent:{}", prefix, convertClazz.getName(), yamlContent);
                return Collections.emptyList();
            }

            return listBindResult.get();
        } catch (IOException e) {
            log.error("[ConfigFileConvertUtil][converterObjectByYaml] yaml配置信息加载失败:{}", getStackTrace(e));
            return Collections.emptyList();
        }
    }

}
